#ifndef DBASE_H
#define DBASE_H
#include <QObject>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QSqlTableModel>
#include <QTableView>
#include <QMessageBox>
#include <QComboBox>
#include <QHeaderView>
#include <QMenu>
enum Identity {
    root,
    admin,
    user
};
struct struct_user {
    int userId;
    QString username;
    QString pass;
    Identity x;
    QString product;
    QString device;
    QString token;
};
class dbase :public QObject
{
    Q_OBJECT
public:
    dbase(QObject *parent = nullptr);
    int iden = 3;
    bool openDb();
    int queryDb(int type,struct_user *list);
    bool insertDb(QString username,QString pass);
    bool deleteDb(QString username);
    bool updateDb1(QString username1,QString username2);//1.用户名 2.密码 3.token
    bool updateDb2(QString username,QString pass);
    bool updateDb3(QString userId,QString product,QString device,QString token);
    bool updataDb4(QString username,QString iden);
    void tableviewInit(QTableView *table);
    void sqlModeInit(QWidget *parent,QTableView *table);
    bool deleteSqlData(int rowCount);
    bool updataSqlData(QString userId,QString ien);
    bool updateSqlData(QString userId,QString product,QString device,QString token);
    void summit();
    void revert();
    Identity mapTo(QString value);
    QString mapTo(Identity value);
private:
    QSqlDatabase db;
    QSqlTableModel *sqlTableModel;
    QTableView *mtable;
    QMenu *m_menu;
    QAction *m_actionAdd ,*m_actionDel;
    QString insert_sql_cmd = "INSERT INTO user(username, pass, identity) VALUES ('%1','%2','user')";//注册
    QString delete_sql_cmd = "DELETE FROM user where userId = '%1'";//删除
    QString update_sql_cmd1 = "UPDATE user SET username = '%1' WHERE (user.username = '%2')";//更改用户名
    QString update_sql_cmd2 = "UPDATE user SET pass = '%1' WHERE (user.username = '%2')";//更改密码
    QString update_sql_cmd3 = "UPDATE user SET product = '%1',device = '%2',token = '%3' WHERE (user.userId = '%4')";//更改token
    QString update_sql_cmd4 = "UPDATE user SET identity = '%1' WHERE (user.userId = '%2')";//更改权限
    QString query_sql_cmd1 = "SELECT * FROM user WHERE username = '%1'";
    QString query_sql_cmd2 = "SELECT * FROM user";//查找全部
private slots:
    void slotAdd();
    void slotDel();
    void slotPopMenu(QPoint p);
};

#endif // DB_H
